let that = null;
import store from "../../utils/factory";
import {
  addOrder,
  payOrder,
  getAdressList,
  batchAdd
} from "../../utils/config";
Page({
  data: {
    detail: {},
    remark: "",
    address: null,
    productTotal: 0,
    orderId: "",
    total: 0,
    express: 0,
    status: "",
    id: "",
    submitStatus: false,
    shopCarList: []
  },
  onLoad: function (e) {
    that = this;
  },
  onShow() {
    //地址
    //订单信息
    let submitOrderDetail = store.get("submitOrderDetail") || {};
    console.log("submitOrderDetail", submitOrderDetail);
    let a = submitOrderDetail.shopCarList;
    console.log(a);
    if (a.length > 0) {
      a.map(item => {
        item.total = (item.today * item.quantity).toFixed(2);
      });
    }
    this.setData({
      total: submitOrderDetail.total,
      express: store.get("express"),
      productTotal: submitOrderDetail.total - store.get("express"),
      shopCarList: a,
      status: submitOrderDetail.status || "",
      id: submitOrderDetail.id || "",
      submitStatus: false
    });
  },
  watchRemark(e) {
    this.setData({
      remark: e.detail.value
    });
  },
  getAdressList() {
    getAdressList().then(res => {
      console.log(res.rows);
      if (res.rows.length > 0) {
        res.rows.map(item => {
          if (item.defaultFlag == true) {
            that.setData({
              address: item
            });
          }
        });
      }
    });
  },
  addAddress() {
    wx.navigateTo({
      url: "../addressHandle/addressHandle?type=add"
    });
  },
  updateAddress() {
    console.log("122");
    wx.navigateTo({
      url: "../addressList/addressList?type=choice"
    });
  },
  pay() {
    this.payOrder(this.data.id);
  },
  payOrder(id) {
    payOrder({
      id: id
    }).then(res => {
      wx.hideLoading();
      this.payWX(res.data);
    });
  },
  payWX(data) {
    wx.requestPayment({
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.packageValue,
      signType: "MD5",
      paySign: data.paySign,
      success: function (res) {
        wx.redirectTo({
          url: `../resultPage/resultPage`
        });
      },
      fail: function (res) {
        wx.redirectTo({
          url: `../orderDetail/orderDetail?id=${
            this.data.orderId
          }`
        });
      },
      complete: function (res) {}
    });
  },
  subscribe() {
    let items = [];
    let tmplIds = ['5F-2HVILaY3A9i4y9aZTB4LTRgdHh5DcrTCfoZtlTqo'];
    wx.requestSubscribeMessage({
      tmplIds: tmplIds,
      success(res) {
        console.log(res);
        wx.showLoading({
          title: "配送单生成中..."
        });
        that.data.shopCarList.map(item => {
          items.push({
            category: item.id,
            quantity: item.quantity
          });
        });
        batchAdd({
            deliveries: items,
            remark: that.data.remark
          })
          .then(res => {
            that.setData({
              submitStatus: false
            })
            wx.hideLoading();
            wx.showModal({
              title: "温馨提示",
              content: "提交配送成功",
              showCancel: false, //是否显示取消按钮
              success: function (res) {
                wx.navigateBack({
                  delta: 1
                });
              }
            });
          })
          .catch(err => {
            wx.hideLoading();
            that.setData({
              submitStatus: false
            })
          });
      },
      fail() {
        wx.hideLoading();
        that.setData({
          submitStatus: false
        })
      }
    })
  },
  addOrder: function (e) {
    if (this.data.submitStatus) {
      return false
    }
    this.setData({
      submitStatus: true
    })
    that.subscribe();
  }
});